package com.flyme.module.base.provider.controller;


import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.module.base.client.api.SysAccountRemoteService;
import com.flyme.module.base.client.dto.SysAccountDto;
import com.flyme.module.base.client.dto.SysThirdLogin;
import com.flyme.module.base.client.entity.SystemAccount;
import com.flyme.module.base.provider.service.SysAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账号管理
 *
 * @author zyf
 */
@Slf4j
@Api(tags = "账号管理")
@RestController
public class SysAccountController implements SysAccountRemoteService {
    @Autowired
    private SysAccountService accountService;

    /**
     * 获取账号登录信息
     *
     * @param username 登录名
     * @return
     */
    @ApiOperation(value = "获取账号信息", notes = "获取账号登录信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", required = true, value = "登录名", paramType = "path")})
    @GetMapping("/account/login")
    @Override
    public ResultBody<SysAccountDto> login(@RequestParam(value = "username") String username) {
        SysAccountDto account = accountService.login(username);
        return ResultBody.success(account);
    }

    /**
     * 修改账号密码
     *
     * @param password
     * @param newPass
     * @return
     */
    @PostMapping("/account/resetPassword")
    @ApiOperation(value = "修改密码")
    public ResultBody<Object> resetPassword(@RequestParam("password") String password, @RequestParam("newPass") String newPass) {
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        Boolean tag = accountService.resetPassword(openUserAuth.getUserId(), password, newPass);
        return ResultBody.ok("修改密码成功").setData(tag);
    }

    /**
     * 修改其他账户密码
     *
     * @param account
     * @return
     */
    @PostMapping("/account/resetOtherPwd")
    @ApiOperation(value = "修改其他账户密码")
    public ResultBody<Object> resetPassword(@RequestParam("accountName") String accountName) {
        Boolean tag = accountService.resetOtherPwd(accountName, CommonConstants.DEF_PWD);
        return ResultBody.ok("密码重置成功").setData(tag);
    }

    /**
     * 注册第三方登录账号
     *
     * @param account
     * @param password
     * @param accountType
     * @return
     */
    @ApiOperation(value = "注册第三方登录账号", notes = "仅限系统内部调用")
    @PostMapping("/account/register/thirdParty")
    @Override
    public ResultBody<SystemAccount> registerThirdPartyAccount(
        @RequestParam(value = "account") String account,
        @RequestParam(value = "userhead") String userhead,
        @RequestParam(value = "nickName") String nickName,
        @RequestParam(value = "accountType") String accountType) {
        SysThirdLogin thirdLogin = new SysThirdLogin();
        thirdLogin.setLoginType(accountType);
        thirdLogin.setIdentifier(account);
        thirdLogin.setUserhead(userhead);
        thirdLogin.setNickname(nickName);
        SystemAccount systemAccount = accountService.registerByThird(thirdLogin);
        return ResultBody.success(systemAccount);
    }


}
